package com.vacation.vacationdemo.controller;

import com.vacation.vacationdemo.service.LoginService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author 杨光
 * @date 2022年03月09日 18:58
 */
@Controller
@RequestMapping("vacation")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping("tologin")
    public String toLogin(){
        return "Login";
    }

    @RequestMapping("toindex")
    public String toIndex(){
        return "Index";
    }


    @RequestMapping("unauth")
    public String toUnauth(){
        return "401";
    }

    @RequestMapping("dologin")
    public String doLogin(String empNo, String empPassword,
                          HttpServletRequest request){

        //用户认证信息
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(
                empNo,
                empPassword
        );

        try {
            //进行验证，这里可以捕获异常，然后返回对应信息
            subject.login(usernamePasswordToken);
        } catch (Exception e) {
            request.setAttribute("errorMsg","账号或密码错误！");
            return "Error";
        }



        return "Index";

//        Employee employee = loginService.doLogin(empNo, empPassword);
//        if(employee==null){
//            request.setAttribute("errorMsg","用户名或密码错误");
//            return "Error";
//        }


        //登录成功，把用户信息放入session
//        HttpSession session = request.getSession();
//        session.setAttribute("loginUser",employee);


//        if(employee.getRoleId()==1){
//            return "forward:/vacation/employee";
//        }
//
//        return "forward:/vacation/search";

    }

//    @RequestMapping("logout")
//    public String logout(HttpServletRequest request){
//        HttpSession session = request.getSession();
//        session.invalidate();//让session失效
//        return "redirect:/vacation/tologin";//重定向
//    }
}
